#include "stdafx.h"
#include "COMWINPSTC.h"


///////////////////////////////////////////////////////////////////////////////
//    OpenServiceProfileSection()
//
//    Parameters
//      pSupObj         Pointer to the provider support object
//      ppProfSectObj   Where we return a pointer to the service profile
//                      section of the provider
//      pfnFreeBuffer   MAPI memory allocation function
//
//    Purpose
//      This function opens the profile section of this service, where the
//      properties of a WINDS provider (AB, MS, or XP) are stored.
//
//    Return Value
//      An HRESULT
//
HRESULT WINAPI OpenServiceProfileSection (LPMAPISUP    pSupObj,
										  LPPROFSECT * ppProfSectObj,
										  LPFREEBUFFER pfnFreeBuffer)
{
	SPropTagArray sptService = { 1, { PR_SERVICE_UID } };
	LPPROFSECT pProvProfSectObj;
	ULONG cValues;
	LPSPropValue pProp;
	// Get the PROVIDER profile section
	// Passing NULL for the lpUid parameter opens the caller's profile section.
	HRESULT hResult = pSupObj->OpenProfileSection (NULL,
		MAPI_MODIFY,
		&pProvProfSectObj);
	if (!hResult)
	{
		// Get the UID of the profile section of the service where this provider is installed
		hResult = pProvProfSectObj->GetProps (&sptService, fMapiUnicode, &cValues, &pProp);
		if (SUCCEEDED(hResult))
		{
			if (S_OK == hResult)
			{
				// Now, with the obtained UID, open the profile section of the service
				ASSERTMSG (PR_SERVICE_UID == pProp->ulPropTag, "Where is PR_SERVICE_UID");
				hResult = pSupObj->OpenProfileSection ((LPMAPIUID)pProp->Value.bin.lpb,
					MAPI_MODIFY,
					ppProfSectObj);
			}
			else
			{
				hResult = E_FAIL;
			}
			pfnFreeBuffer (pProp);
		}
		pProvProfSectObj->Release();
	}
	TraceResult ("OpenServiceProfileSection", hResult);
	return hResult;
}
